package cn.xuanxuanyuhua.crm.shiro.filter;

import cn.xuanxuanyuhua.crm.util.JSONResult;
import cn.xuanxuanyuhua.crm.util.JSONUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

@Component("formFilter")
public class CRMFormFilter extends FormAuthenticationFilter{

    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
        response.setContentType("text/json;charset=utf8");
        response.getWriter().println(JSONUtil.toJSONString(new JSONResult()));
        return false;
    }

    @Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
        e.printStackTrace();
        try {
            response.setContentType("text/json;charset=utf8");
            response.getWriter().println(JSONUtil.toJSONString(new JSONResult().mark("账号密码不匹配")));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return false;
    }
}
